﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AtletIS.Shared.Helpers;

namespace AtletIS.Shared.Forms
{
    public partial class frmConnections : Form
    {
        private bool bValid = false;
        
        public frmConnections()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.Connections64;
        }
        
        // Test connection
        private void tsbTest_Click(object sender, EventArgs e)
        {
            string sServer = string.Empty;
            if (txtComputer.Text == string.Empty)
                sServer = txtServer.Text;
            else
                sServer = txtComputer.Text + "\\" + txtServer.Text;

            string connString = "Server=" + sServer + "; Database=" + txtDatabase.Text + "; User Id=atletis; Password=atletis;";
            SqlConnection sqlConn = new SqlConnection(connString);
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                sqlConn.Open();
                if (sqlConn.State == System.Data.ConnectionState.Open)
                {
                    bValid = true;
                    MessageBox.Show("Pripojenie bolo úspešné.", "Connections", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch
            {
                bValid = false;
                MessageBox.Show("Pripojenie zlyhalo.", "Connections", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        // Save connection
        private void tsbCreate_Click(object sender, EventArgs e)
        {
            if (!bValid)
            {
                MessageBox.Show("Najskôr otestujte pripojenie.", "Connections", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (Helpers.ConnectionHelper.SetConnectionStrings(txtComputer.Text, txtServer.Text, txtDatabase.Text))
            {
                MessageBox.Show("Uloženie prebehlo úspešne.", "Connections", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
                MessageBox.Show("Uloženie zlyhalo.", "Connections", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        // Koniec
        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }




    }
}
